package com.performance;

import java.util.Collections;
import java.util.List;

import com.bean.Customer;
import com.bl.service.Service;
import com.bl.service.customer.impl.CustomerServiceWithLogging;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import org.junit.Before;

/**
 * @author apodoprigora
 */
public class SmokeBase {

    protected Service<Customer> customerService;
    protected List<Customer> customers;


    @Before
    public void setUp() {
        customerService = new CustomerServiceWithLogging();
    }


    protected void testSmokeCustomer(final Customer aCustomer) throws Exception {
        assertNull(aCustomer.getId());
        insertOneCustomer(aCustomer);
        findOneCustomer(aCustomer);
        deleteOneCustomer(aCustomer);
    }


    private void deleteOneCustomer(final Customer aCustomer) {
        final Customer foundCustomer;
        customerService.deleteFromDbOneByOne(Collections.singletonList(aCustomer));
        foundCustomer = customerService.findById(aCustomer.getId());
        assertNull("There should be no customer", foundCustomer);
    }


    private void findOneCustomer(final Customer aCustomer) {
        final Customer foundCustomer = customerService.findById(aCustomer.getId());
        assertNotNull("There should be found a customer", foundCustomer);
        assertNotNull("The found customer should have defined id", foundCustomer.getId());
        System.out.println("Found customer : " + foundCustomer);
    }


    private void insertOneCustomer(final Customer aCustomer) {
        customerService.insertToDbOneByOne(Collections.singletonList(aCustomer));
        System.out.println("Inserted customer : " + aCustomer);
    }


    public void testSmokeCustomers(final List<Customer> aCustomers) throws Exception {
        for (final Customer customer : aCustomers) {
            assertNull(customer.getId());
        }
        insertManyCustomers(aCustomers);
        findManyCustomers(aCustomers);
        deleteManyCustomers(aCustomers);
    }


    private void deleteManyCustomers(final List<Customer> aCustomers) {
        customerService.deleteUsingBatches(aCustomers);
        for (final Customer customer : aCustomers) {
            final Customer foundCustomer = customerService.findById(customer.getId());
            assertNull("There should be no customer", foundCustomer);
        }
    }


    private void findManyCustomers(final List<Customer> aCustomers) {
        for (final Customer customer : aCustomers) {
            final Customer foundCustomer = customerService.findById(customer.getId());
            assertNotNull("There should be found a customer", foundCustomer);
            assertNotNull("The found customer should have defined id", foundCustomer.getId());
            System.out.println("Found customer : " + foundCustomer);
        }
    }


    private void insertManyCustomers(final List<Customer> aCustomers) {
        customerService.insertUsingBatches(aCustomers);
        for (final Customer customer : aCustomers) {
            System.out.println("Inserted customer : " + customer);
        }
    }

}
